package cn.yr.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.yr.common.core.constant.UserConstants;
import cn.yr.common.core.domain.entity.SysDictType;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.core.manager.AuthUserManager;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import cn.yr.system.service.ISysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysDictTypeController
 * @description 数据字典信息
 * @author Lang
 * @date 2020-07-13 11:23
 */
@RestController
@RequestMapping("/sys/dict/type")
public class SysDictTypeController {

    @Resource
    private ISysDictTypeService dictTypeService;

    @Resource
    private AuthUserManager authUserManager;

    @PreAuthorize(hasPermi = "system:menu:list")
    @RequestMapping("/list")
    public Ret list(@RequestBody PageVo<SysDictType> pageVo) {
        Map<String, Object> params = pageVo.getParams();
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(params.get("status")),"status",params.get("status"));
        wrapper.like(ObjectUtil.isNotEmpty(params.get("dictName")),"dict_name",params.get("dictName"));
        wrapper.like(ObjectUtil.isNotEmpty(params.get("dictType")),"dict_type",params.get("dictType"));
        wrapper.between(ObjectUtil.isNotEmpty(params.get("startCreateTime")),"create_time",params.get("startCreateTime"),params.get("endCreateTime"));
        wrapper.orderByDesc("create_time");
        PageVo<SysDictType> page = dictTypeService.page(pageVo, wrapper);
        return Ret.success( page);
    }


    /**
     * 查询字典类型详细
     */
    @PreAuthorize(hasPermi = "system:menu:query")
    @RequestMapping("/info/{dictId}")
    public Ret getInfo(@PathVariable Long dictId) {
        return Ret.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize(hasPermi = "system:menu:add")
    @RequestMapping("/add")
    public Ret add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return Ret.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(authUserManager.getUserName());
        return Ret.success(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize(hasPermi = "system:menu:edit")
    @RequestMapping("/edit")
    public Ret edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return Ret.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(authUserManager.getUserName());
        return Ret.success(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize(hasPermi = "system:menu:remove")
    @RequestMapping("/remove/{dictIds}")
    public Ret remove(@PathVariable Long[] dictIds) {
        return Ret.success(dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize(hasPermi = "system:menu:list")
    @RequestMapping("/clearCache")
    public Ret clearCache() {
        dictTypeService.clearCache();
        return Ret.success();
    }

    /**
     * 获取字典选择框列表
     */
    @PreAuthorize(hasPermi = "system:menu:list")
    @RequestMapping("/optionselect")
    public Ret optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return Ret.success(dictTypes);
    }
}
